/*
 * Copyright (C) 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * This code is not supported by Google
 *
 */

/*  
 * Sample code to issue a simple GCS query using the Beta Google Client Libraries:
 * http://code.google.com/p/google-api-java-client/
 * 
 * javac -cp .:google-api-client-1.6.0-beta.jar:
 * 			   google-api-services-shopping-v1-1.3.0-beta.jar:
 *             google-collections-1.0.jar:
 *             google-http-client-1.6.0-beta.jar:
 *             jackson-core-asl-1.9.1.jar GCSSearch.java
 * java -cp .:google-api-client-1.6.0-beta.jar:
 * 			  google-api-services-shopping-v1-1.3.0-beta.jar:
 *            google-collections-1.0.jar:
 *            google-http-client-1.6.0-beta.jar:
 *            jackson-core-asl-1.9.1.jar GCSSearch
 * This code and the libraries above is NOT supported by Google Enterprise.
 */

import java.io.IOException;
import com.google.api.services.shopping.*;
import com.google.api.services.shopping.model.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.http.json.*;
import java.util.logging.*;

public class GCSSearch {

	public static void main(String[] args) {

		GCSSearch g = new GCSSearch();
	}
	
	public GCSSearch(){
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setLevel(Level.ALL);
        consoleHandler.setFormatter(new SimpleFormatter());
	    Logger logger = Logger.getLogger("com.google.api.client");
	    logger.setLevel(Level.ALL);
        logger.addHandler(consoleHandler);
       
        Shopping.Builder sb =Shopping.builder(new NetHttpTransport(), new JacksonFactory());
        
 	    Shopping shopping = sb.build();
 	    
		   try {
		 	    Shopping.Products.List req = shopping.products().list("cx:YOUR_CX")
		        .setQ("GCS")
		        .setCountry("us")
		        .setLanguage("en")
		        .setStartIndex(1L)
		        .setMaxResults(10L)
		        .setSafe(false)
		        .setSpellingEnabled(true)
		        .setSpellingUseGcsConfig(true)
		        .setPromotionsEnabled(true)
		        .setPromotionsUseGcsConfig(true)
		        .setRedirectsEnabled(true)
		        .setRedirectsUseGcsConfig(true)
		        .setFacetsEnabled(true)
		        .setFacetsUseGcsConfig(true); 
		 	req.setKey("YOUR_KEY");
		 	
			Products ps = req.execute();
			System.out.println(ps.getTotalItems());
			for (Product p : ps.getItems() ){
				System.out.println(p.getProduct().getTitle());
			}
			
		   } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
